package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.enterprise.models.QualificationCertificate;
import com.budwk.app.enterprise.services.QualificationcertificateService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/enterprise/enterprise/qualification/certificate")
@ApiDefinition(tag = "资质证书信息")
@Slf4j
public class QualificationcertificateController{
    @Inject
    private QualificationcertificateService    wxQualificationCertificateService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询资质证书信息")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = QualificationCertificate.class
    )
    @ApiResponses(
    implementation = QualificationCertificate.class
    )
    @SaCheckPermission("enterprise.enterprise.wx.qualification.certificate")
    public Result<?> list(@Param("..")QualificationCertificate bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getDecideBookNumber() !=null && Strings.isNotBlank(bean.getDecideBookNumber().toString())) {
            cnd.and("decideBookNumber","=",bean.getDecideBookNumber());
        }
        
        if (bean.getNameLicenseDocument() !=null && Strings.isNotBlank(bean.getNameLicenseDocument().toString())) {
            cnd.and("nameLicenseDocument","=",bean.getNameLicenseDocument());
        }
        
        if (bean.getValidFrom() !=null && Strings.isNotBlank(bean.getValidFrom().toString())) {
            cnd.and("validFrom","=",bean.getValidFrom());
        }
        
        if (bean.getValidUntil() !=null && Strings.isNotBlank(bean.getValidUntil().toString())) {
            cnd.and("validUntil","=",bean.getValidUntil());
        }
        
        if (bean.getLicensingAuthority() !=null && Strings.isNotBlank(bean.getLicensingAuthority().toString())) {
            cnd.and("licensingAuthority","=",bean.getLicensingAuthority());
        }
        
        if (bean.getLicensedContent() !=null && Strings.isNotBlank(bean.getLicensedContent().toString())) {
            cnd.and("licensedContent","=",bean.getLicensedContent());
        }
        
        if (bean.getDataSource() !=null && Strings.isNotBlank(bean.getDataSource().toString())) {
            cnd.and("dataSource","=",bean.getDataSource());
        }
        
        if (bean.getCertificateNumber() !=null && Strings.isNotBlank(bean.getCertificateNumber().toString())) {
            cnd.and("certificateNumber","=",bean.getCertificateNumber());
        }
        
        if (bean.getCertificateStatus() !=null && Strings.isNotBlank(bean.getCertificateStatus().toString())) {
            cnd.and("certificateStatus","=",bean.getCertificateStatus());
        }
        
        if (bean.getDateIssuance() !=null && Strings.isNotBlank(bean.getDateIssuance().toString())) {
            cnd.and("dateIssuance","=",bean.getDateIssuance());
        }
        
        if (bean.getCertificateExpirationDate() !=null && Strings.isNotBlank(bean.getCertificateExpirationDate().toString())) {
            cnd.and("certificateExpirationDate","=",bean.getCertificateExpirationDate());
        }
        
        if (bean.getDateInitialCertification() !=null && Strings.isNotBlank(bean.getDateInitialCertification().toString())) {
            cnd.and("dateInitialCertification","=",bean.getDateInitialCertification());
        }
        
        if (bean.getDateInformationReporting() !=null && Strings.isNotBlank(bean.getDateInformationReporting().toString())) {
            cnd.and("dateInformationReporting","=",bean.getDateInformationReporting());
        }
        
        if (bean.getCertificationProject() !=null && Strings.isNotBlank(bean.getCertificationProject().toString())) {
            cnd.and("certificationProject","=",bean.getCertificationProject());
        }
        
        if (bean.getCertificationBasis() !=null && Strings.isNotBlank(bean.getCertificationBasis().toString())) {
            cnd.and("certificationBasis","=",bean.getCertificationBasis());
        }
        
        if (bean.getNumberSupervision() !=null && Strings.isNotBlank(bean.getNumberSupervision().toString())) {
            cnd.and("numberSupervision","=",bean.getNumberSupervision());
        }
        
        if (bean.getNumberRecertifications() !=null && Strings.isNotBlank(bean.getNumberRecertifications().toString())) {
            cnd.and("numberRecertifications","=",bean.getNumberRecertifications());
        }
        
        if (bean.getBusinessScopeCertification() !=null && Strings.isNotBlank(bean.getBusinessScopeCertification().toString())) {
            cnd.and("businessScopeCertification","=",bean.getBusinessScopeCertification());
        }
        
        if (bean.getMultiplePlaces() !=null && Strings.isNotBlank(bean.getMultiplePlaces().toString())) {
            cnd.and("multiplePlaces","=",bean.getMultiplePlaces());
        }
        
        if (bean.getPlaceCoveredCertification() !=null && Strings.isNotBlank(bean.getPlaceCoveredCertification().toString())) {
            cnd.and("placeCoveredCertification","=",bean.getPlaceCoveredCertification());
        }
        
        if (bean.getRecognitionMarkCertificate() !=null && Strings.isNotBlank(bean.getRecognitionMarkCertificate().toString())) {
            cnd.and("recognitionMarkCertificate","=",bean.getRecognitionMarkCertificate());
        }
        
        if (bean.getCertificatedOrganization() !=null && Strings.isNotBlank(bean.getCertificatedOrganization().toString())) {
            cnd.and("certificatedOrganization","=",bean.getCertificatedOrganization());
        }
        
        if (bean.getLicensedOrganizationCode() !=null && Strings.isNotBlank(bean.getLicensedOrganizationCode().toString())) {
            cnd.and("licensedOrganizationCode","=",bean.getLicensedOrganizationCode());
        }
        
        if (bean.getOrganizationCountryRegion() !=null && Strings.isNotBlank(bean.getOrganizationCountryRegion().toString())) {
            cnd.and("organizationCountryRegion","=",bean.getOrganizationCountryRegion());
        }
        
        if (bean.getCertificatedOrganizationNumber() !=null && Strings.isNotBlank(bean.getCertificatedOrganizationNumber().toString())) {
            cnd.and("certificatedOrganizationNumber","=",bean.getCertificatedOrganizationNumber());
        }
        
        if (bean.getCertificatedOrganizationAddress() !=null && Strings.isNotBlank(bean.getCertificatedOrganizationAddress().toString())) {
            cnd.and("certificatedOrganizationAddress","=",bean.getCertificatedOrganizationAddress());
        }
        
        if (bean.getIssuingAgencyName() !=null && Strings.isNotBlank(bean.getIssuingAgencyName().toString())) {
            cnd.and("issuingAgencyName","=",bean.getIssuingAgencyName());
        }
        
        if (bean.getIssuingAgencyNumber() !=null && Strings.isNotBlank(bean.getIssuingAgencyNumber().toString())) {
            cnd.and("issuingAgencyNumber","=",bean.getIssuingAgencyNumber());
        }
        
        if (bean.getIssuingValidityPeriod() !=null && Strings.isNotBlank(bean.getIssuingValidityPeriod().toString())) {
            cnd.and("issuingValidityPeriod","=",bean.getIssuingValidityPeriod());
        }
        
        if (bean.getIssuingAgencyStatus() !=null && Strings.isNotBlank(bean.getIssuingAgencyStatus().toString())) {
            cnd.and("issuingAgencyStatus","=",bean.getIssuingAgencyStatus());
        }
        
        if (bean.getIssuingAgencyWebsite() !=null && Strings.isNotBlank(bean.getIssuingAgencyWebsite().toString())) {
            cnd.and("issuingAgencyWebsite","=",bean.getIssuingAgencyWebsite());
        }
        
        if (bean.getIssuingAgencyAddress() !=null && Strings.isNotBlank(bean.getIssuingAgencyAddress().toString())) {
            cnd.and("issuingAgencyAddress","=",bean.getIssuingAgencyAddress());
        }
        
        if (bean.getIssuingBusinessScope() !=null && Strings.isNotBlank(bean.getIssuingBusinessScope().toString())) {
            cnd.and("issuingBusinessScope","=",bean.getIssuingBusinessScope());
        }
        
        return Result.data(   wxQualificationCertificateService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增资质证书信息")
    @ApiFormParams(
        implementation = QualificationCertificate.class
    )
    @ApiResponses
    @SLog(tag = "新增资质证书信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.qualification.certificate.create")
    public Result<?> create(@Param("..")QualificationCertificate    wxQualificationCertificate, HttpServletRequest req) {
           wxQualificationCertificate.setCreatedBy(SecurityUtil.getUserId());
           wxQualificationCertificateService.insert(   wxQualificationCertificate);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改资质证书信息")
    @ApiFormParams(
        implementation = QualificationCertificate.class
    )
    @ApiResponses
    @SLog(tag = "修改资质证书信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.qualification.certificate.update")
    public Result<?> update(@Param("..")QualificationCertificate    wxQualificationCertificate, HttpServletRequest req) {
           wxQualificationCertificate.setUpdatedBy(SecurityUtil.getUserId());
           wxQualificationCertificateService.updateIgnoreNull(   wxQualificationCertificate);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取资质证书信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.enterprise.wx.qualification.certificate")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        QualificationCertificate    wxQualificationCertificate =    wxQualificationCertificateService.fetch(id);
        if (   wxQualificationCertificate == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(   wxQualificationCertificate);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除资质证书信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除资质证书信息", value = "${args[0]}")
    @SaCheckPermission("enterprise.enterprise.wx.qualification.certificate.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        QualificationCertificate    wxQualificationCertificate =    wxQualificationCertificateService.fetch(id);
        if (   wxQualificationCertificate == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
           wxQualificationCertificateService.delete(id);
        req.setAttribute("SlogMsg",    wxQualificationCertificate.getId());
        return Result.success();
    }
}
